home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2587 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.2 KB

  1. Path: locutus.rchland.ibm.com!usenet
  2. From: pstaite@vnet.ibm.com
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: overloading []
  5. Date: 18 Jan 1996 14:41:58 GMT
  6. Organization: IBM OS/2 Device Driver Development  Rochester, MN
  7. Message-ID: <4dlm7m$on9@locutus.rchland.ibm.com>
  8. References: <4dgjbl$6i3@news1.goodnet.com> <4dh86s$bfi@locutus.rchland.ibm.com> <ENNO.96Jan17200900@kitz.inferenzsysteme.informatik.th-darmstadt.de>
  9. Reply-To: pstaite@vnet.ibm.com
  10. NNTP-Posting-Host: warpone.rchland.ibm.com
  11. X-Newsreader: IBM NewsReader/2 v1.2
  12.  
  13. In <ENNO.96Jan17200900@kitz.inferenzsysteme.informatik.th-darmstadt.de>, enno@inferenzsysteme.informatik.th-darmstadt.de (Enno Sandner) writes:
  14. >In article <4di9v9$8k6@news.bridge.net> David Byrden <100101.2547@compuserve.com> writes:
  15. >
  16. >   >> No, you could do:
  17. >
  18. >   >>     foo& f( *new foo );
  19. >   >>  later on:
  20. >
  21. >   >>     delete &f;
  22. >
  23. >
  24. >   Phil, I believe the C++ standards committee have decided to play safe by 
  25. >   declaring this kind of code as "ill-formed". In other words, a 'perfect' 
  26. >   C++ compiler need not behave as you want here.
  27.  
  28. Hmm, I'll have to look through the DWP and see if this will become
  29. illegal, I hope not.
  30.  
  31. Actually, if I were writing the code to use I wouldn't do it quite so 
  32. briefly.  When I post code to the net I usually don't include much (any)
  33. error checking just to keep it short.  If I were really going to do 
  34. something like this I'd code:
  35.  
  36.     foo* p( new foo );
  37.     if( ! p ) {
  38.         // do something, probably error
  39.         // out of the function
  40.     }
  41.     foo& f( *p );
  42.     f[ 0 ] = // etc. etc.
  43.  
  44.     delete p;
  45.     // end of scope
  46.  
  47.  
  48. Note, you have to be careful where you do the delete p since you're 
  49. pulling the rug out from underneath the ref f.  Also, if your compiler 
  50. will throw an exception from new instead of returning NULL that makes 
  51. life easier.
  52.  
  53. If the function were short and I was only referencing the foo a couple 
  54. of times I wouldn't bother with the reference, I'd just use (*p)[].  The
  55. ref is just there for readability, but it's setup requires some code 
  56. that detracts from readability (IMHO).  So I'd have to be looking at a 
  57. lot of (*p)[] constructs before I'd sign up for the ref.
  58.  
  59.  
  60. Phil Staite, team OS/2
  61. internet: pstaite@vnet.ibm.com  internal: pstaite@rchland
  62.  
  63.